#include<iostream>

using namespace std;

template<class T>
class Stack
{
private:
	
	class Node
	{
	public:
		T _value;
		Node* _next;
		Node() : _value(), _next(nullptr) {}
		Node(T value) : _value(value), _next(nullptr) {}
	};
public:
	Stack()
		:top_index(nullptr)
	{}     //构造函数
	~Stack()                  //析构函数
	{
		Node* cur = top_index;
		while (top_index != nullptr)
		{
			top_index = top_index->_next;
			delete cur;
			cur = top_index;
		}
	}
	void push(T x)                   //入栈操作
	{
		Node* newnode = new Node(x);
		newnode->_next = top_index;
		top_index = newnode;
	}

	T pop()                           //出栈操作
	{
		if (empty())throw std::out_of_range("Stack is empty");
		Node* delnode = top_index;
		T value = delnode->_value;
		top_index = top_index->_next;
		delete delnode;
		return value;
	}

	T top()                       //取栈顶操作
	{
		if (empty())throw std::out_of_range("Stack is empty");
		return top_index->_value;
	}

	bool empty()                       //判空操作
	{
		return top_index == nullptr;
	}
private:
	Node* top_index;
};

int main()
{

	Stack<int> s;          //定义一个int类型的栈，命名为s

	s.push(1);           //入栈
	s.push(2);

	cout<<s.pop()<<endl;               //出栈

	cout<<s.top()<<endl;               //取栈顶，返回栈顶元素
	s.pop();
	cout<<s.top()<<endl;             //判空，返回bool值

	return 0;
}

